package com.itheima.controller;

import com.itheima.Service.SetmealService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增套餐
     */
    @PostMapping("/add")
    public Result listsetmeal(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds) {
        log.info("新增套餐:{}", setmeal);
        String key = "photo1";
        setmealService.list(setmeal, checkgroupIds);
        redisTemplate.opsForSet().add(key, setmeal.getImg());

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS, null);
    }

    /**
     * 分页查询套餐
     */
    @PostMapping("/findPage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean ) {
        log.info("分页查询套餐:{}",queryPageBean);
        PageResult P=setmealService.listpage(queryPageBean);
        log.info("集合:{}",P);
        return P;
    }
    /**
     * 根据id查询套餐表
     */
    @GetMapping("/findById")
    public Result<CheckItem> readcheck(Integer id){
        log.info("查看指定套餐:{}",id);
        CheckItem checkItem=setmealService.readcheck(id);
        return Result.success(checkItem);
    }
    /**
     * 删除套餐
     */
    @GetMapping("/delete")
    public Result delect(Integer id){
        log.info("删除套餐:{}",id);
        setmealService.delectcheck(id);
        return new Result(true,"删除套餐成功",null);
    }
    /**
     * 编辑套餐
     */
    @PostMapping("/edit")
    public Result updatecheck(@RequestBody Setmeal setmeal,@RequestParam List<Integer>checkGroupIds){
        log.info("修改套餐:{}",setmeal);
        String key="photo2";
        setmealService.updatecheck(setmeal,checkGroupIds);
        redisTemplate.opsForSet().add(key,setmeal.getImg());
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS,null);

    }

}
